// pages/car/accredit/bound/bound.js
const app = getApp()

Page({

    data: {
        bound:{},
        // imgList:'/images/idImg.png',
        imgListTwo: '/images/driverImg.png',
        imgListThree: '/images/photo.png',
        imgListFour: '/images/drivinglicense.png',
        // imgOne: '',
        imgTwo: '',
        imgThree: '',
        imgFour:'',
        is_sendcode: false,// 60s
        time_count: 60,
        name: '',//姓名
        phone:'',//手机号
        code:'',//验证码
        avatarUrl: '',//头像
        plate:'',//车牌号
        open_id: ''//用户id
    },
    onLoad: function (options) {
      this.setData({
        avatarUrl: options.avatarUrl // 头像
      })
    },
    // 上传图片
    uploadImg(e){
      let cramer = e.currentTarget.dataset.type;
      wx.chooseImage({
        sizeType: "compressed", //压缩图
        count: 1,
        success: (res) => {
          console.log(res)
          let path = res.tempFilePaths;
          if(cramer == 1){
            this.setData({
              imgListTwo: path[0]
            })
          }else if(cramer == 2){
            this.setData({
              imgListThree: path[0]
            })
          } else if(cramer == 3){
            this.setData({
              imgListFour: path[0]
            })
          }
          wx.uploadFile({
            url: app.globalData.url + 'api/v1/file/files',
            method: 'POST',
            filePath: path[0],
            name: 'file',
            success: res => {
              // console.log(res)
              let arr = JSON.parse(res.data);
              // console.log(arr)
              if(cramer == 1){
                this.setData({
                  imgTwo: arr.data.url
                })
              }else if(cramer == 2){
                this.setData({
                  imgThree: arr.data.url
                })
              }
              else if(cramer == 3){
                this.setData({
                  imgFour: arr.data.url
                })
              }
            },
            fail: err => {
              console.log(err)
            }
          })
        }
      })
    },
    //  姓名
    inputName(event){
        // console.log(event);
        this.setData({
            name: event.detail.value
        })
    },
    // 手机号
    inputNumber(event){
        // console.log(event);
        this.setData({
            phone: event.detail.value
        })
    },
    // 验证码
    inputCode(event){
      // console.log(event);
      this.setData({
        code: event.detail.value
      })
    },
    // 车牌号
    inputPlateNumber(event){
      // console.log(event);
      this.setData({
        plate: event.detail.value
      })
    },
    // 验证码
    code(){
      if(this.data.phone == ""){
        wx.showToast({
          title: '请输入您的号码',
          icon: 'none',
          mask: true
        })
        return false;
      }
      wx.request({
        url: app.globalData.url + 'api/v1/owner/login/owner-code',
        method: 'GET',
        data:{
          phone: this.data.phone
        },
        success: res => {
          // console.log(res)
          if(res.data.code == 200){
            this.getSendCode();
            wx.showToast({
              title: '发送成功',
              icon: 'none',
              mask: true
            })
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              mask: true
            })
          }
        },
        fail: err => {
          console.log(err)
        }
      })   
    },
    // 60s
    getSendCode() {
      const TIME_COUNT = 60;
      if (!this.timer) {
        this.setData({
          time_count: TIME_COUNT,
          is_sendcode: true
        })
        this.data.timer = setInterval(() => {
          if (this.data.time_count > 0 && this.data.time_count <= TIME_COUNT) {
              this.setData({
                time_count: this.data.time_count-1
              })
          } else {
              this.setData({
                is_sendcode: false,
                timer: null
              })
              clearInterval(this.data.timer);
          }
        }, 1000);
      }
    },
  // 提交 审核成功跳转首页
  submit(){
    wx.showLoading({
      title: '加载中···',
    }) 
    if ( this.data.imgTwo == "" ){
      wx.showToast({
        title: '请上传驾驶证正面',
        icon: 'none',
      })
      return false
    } else if ( this.data.imgThree == "" ){
      wx.showToast({
        title: '请上传本人照片',
        icon: 'none',
      })
      return false
    } else if ( this.data.imgFour == "" ){
      wx.showToast({
        title: '请上传行驶证',
        icon: 'none',
      })
      return false
    } else if ( this.data.name == "" ){
      wx.showToast({
        title: '请输入您的姓名',
        icon: 'none',
      })
      return false
    } else if ( !/^[\u4e00-\u9fa5]{0,16}$/.test(this.data.name) ){
      wx.showToast({
        title: '请正确输入您的姓名',
        icon: 'none',
      })
      return false
    } else if ( this.data.phone == "" ){
      wx.showToast({
        title: '请输入您的号码',
        icon: 'none',
      })
      return false
    } else if (!/^1[3456789]\d{9}$/.test(this.data.phone) ){
      wx.showToast({
        title: '手机号格式不正确',
        icon: 'none',
      })
      return false
    } else if ( this.data.code == "" ){
      wx.showToast({
        title: '请输入您的验证码',
        icon: 'none'
      })
      return false
    } else if ( this.data.plate == ''){
      wx.showToast({
        title: '请输入您的车牌号',
        icon: 'none',
      })
      return false
    } else if(!/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[警京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{0,1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/.test( this.data.plate)){
      wx.showToast({
        title: "请输入正确的车牌号",
        icon: "none",
      })
      return false
    }
    wx.request({
      url: app.globalData.url + 'api/v1/owner/login/register',
      method: 'POST',
      data:{
        open_id: wx.getStorageSync('openid'),
        img: wx.getStorageSync('headImg'),
        // positive: this.data.imgOne,
        driver: this.data.imgTwo,
        picture: this.data.imgThree,
        driving: this.data.imgFour,
        name: this.data.name,
        phone: this.data.phone,
        code: this.data.code,
        plate: this.data.plate
      },
      success: res => {
        // console.log(res.data.message)
        if (res.data.code == 200){
          setTimeout( () => {
            wx.showToast({
              title: '提交成功',
            })
          },500)
          setTimeout(function(){
            wx.reLaunch({
              url: '/pages/car/index/index',
            })
          },1000)
        } else {
          setTimeout( () => {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              mask: true
            })
          },500)
        }
      },
      fail: err => {
        console.log(err)
      },
      complete: () => {
        wx.hideLoading()
      }
    })     
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  } 
})